//----------------------------------Include-------------------------------------
#include <string.h>
#include <stdlib.h>
#include "mcu.h"
#include "glbvar.h"
//------------------------------------------------------------------------------
//global var defined in this file
#define  DBG_GLOBALS
#include "dbg.h"
//------------------------------------------------------------------------------

//---------------------------------Global Var-----------------------------------
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
//for rx
#define RXBUF_LEN                 (128)
//------------------------------------------------------------------------------
//for tx
#define _tx_data(p, l)            do{ mcu_uart1_dma_tx((char*)p, l); }while(0)
#define TXBUF_LEN                 (128)
//------------------------------------------------------------------------------

//------------------------------------Type--------------------------------------
typedef struct{
  unsigned int   cnt;
  unsigned int   done;
  char           data[RXBUF_LEN];
}t_rcv;
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
//for rx
static t_rcv            nt_rcv;
static t_rcv            nt_buf;
//for tx
static char             na_txbuf[TXBUF_LEN];
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
static void _tx_str(char* s)
{
  strcpy(na_txbuf, s);
  _tx_data(na_txbuf, strlen(na_txbuf));
}
//------------------------------------------------------------------------------
static char* _check_rx(uint32_t* sz)
{
  *sz = 0;
  if(!nt_rcv.done) return NULL;
  if(!nt_rcv.cnt ) return NULL;
  *sz = nt_rcv.cnt;
  return &nt_rcv.data[0];
}

static void _clear_rx(void)
{
  nt_rcv.done = 0;
  nt_rcv.cnt  = 0;
}
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
//for DMA receive
uint32_t dbg_get_rcv_addr(void)
{
  return (uint32_t)&nt_buf.data[0];
}

uint32_t dbg_get_rcv_size(void)
{
  return RXBUF_LEN;
}

void it_dbg_dma_rcv_done(uint32_t cnt)
{
  nt_rcv.cnt  = cnt;
  nt_rcv.done = 1;
  memcpy(&nt_rcv.data[0], &nt_buf.data[0], cnt);
}
//------------------------------------------------------------------------------
void dbg_cyc(void)
{
  char*     p;
  uint32_t  sz;
  
  p = _check_rx(&sz);
  if(p){
    _clear_rx();
  }
  
}
//-------------------------------------EOF--------------------------------------


#if 0
//for interrupt receive
void it_dbg_rcv_one_byte(unsigned char c)
{
  if(nt_rcv.cnt >= RXBUF_LEN){
    nt_rcv.done = 0;
    nt_rcv.cnt  = 0;
    return;
  }
  
  nt_rcv.data[nt_rcv.cnt++] = c;
}

void it_dbg_rcv_done(void)
{
  nt_rcv.done = 1;
}
#endif




